1
|
|
|
const { Menu } = require('electron'); |
2
|
|
|
const menuStructure = require("./data/menu.json"); |
|
|
|
|
3
|
|
|
const _ = require("lodash"); |
|
|
|
|
4
|
|
|
|
5
|
|
|
module.exports = class AppMenu { |
6
|
|
|
constructor(app) { |
7
|
|
|
this.app = app; |
8
|
|
|
this.rebuildMenu(); |
9
|
|
|
|
10
|
|
|
app.store.onDidChange('recentDocs', this.onRecentDocsChange.bind(this)); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
// Determines if a menu item can completely skip check or not |
14
|
|
|
doMenuItemSkip(obj) { |
15
|
|
|
// Is it not the wrong platform |
16
|
|
|
if (obj.platform !== undefined && |
17
|
|
|
process.platform !== obj.platform) |
18
|
|
|
return true; |
|
|
|
|
19
|
|
|
|
20
|
|
|
if (obj.notPlatform !== undefined && |
21
|
|
|
process.platform === obj.notPlatform) |
22
|
|
|
return true; |
|
|
|
|
23
|
|
|
|
24
|
|
|
// Is it the wrong build |
25
|
|
|
if (obj.env === "dev" && |
26
|
|
|
this.app.isDev !== true) |
27
|
|
|
return true; |
|
|
|
|
28
|
|
|
|
29
|
|
|
if (obj.env === "prod" && |
30
|
|
|
this.app.isDev === true) |
31
|
|
|
return true; |
|
|
|
|
32
|
|
|
|
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
_rebuildMenu(arr) { |
37
|
|
|
for (var i = 0; i < arr.length; i++) { |
38
|
|
|
const obj = arr[i]; |
39
|
|
|
|
40
|
|
|
// Is not designated platform |
41
|
|
|
if (this.doMenuItemSkip(obj)) { |
42
|
|
|
arr.splice(i, 1); |
43
|
|
|
i--; |
|
|
|
|
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (obj.placeholder !== undefined) { |
48
|
|
|
arr.splice(i, 1); |
49
|
|
|
i--; |
|
|
|
|
50
|
|
|
|
51
|
|
|
if (obj.placeholder === "recentList") |
52
|
|
|
arr.splice(i + 1, 0, ...this.recentFilesStructure); |
|
|
|
|
53
|
|
|
|
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (obj.trigger !== undefined) { |
58
|
|
|
obj.click = () => { |
59
|
|
|
if (Array.isArray(obj.triggerData)) |
60
|
|
|
this.app.emit(`menu-${obj.trigger}`, ...obj.triggerData); |
|
|
|
|
61
|
|
|
else |
62
|
|
|
this.app.emit(`menu-${obj.trigger}`, obj.triggerData); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (obj.submenu !== undefined) { |
67
|
|
|
this._rebuildMenu(obj.submenu); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
get recentFilesStructure() { |
73
|
|
|
const store = this.app.store; |
74
|
|
|
const structure = []; |
75
|
|
|
|
76
|
|
|
// Grab recent docs list |
77
|
|
|
const recentDocs = store.get('recentDocs', []); |
78
|
|
|
|
79
|
|
|
for (let i = 0; i < recentDocs.length; i++) { |
80
|
|
|
const recentDoc = recentDocs[i]; |
81
|
|
|
|
82
|
|
|
structure.push({ |
83
|
|
|
label: `${recentDoc}`, |
84
|
|
|
trigger: "openRecentDocs", |
85
|
|
|
triggerData: i, |
86
|
|
|
accelerator: `CommandOrControl+Shift+${i}` |
87
|
|
|
}); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return structure; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
rebuildMenu() { |
94
|
|
|
this.menuStructure = _.cloneDeep(menuStructure); |
|
|
|
|
95
|
|
|
this._rebuildMenu(this.menuStructure); |
96
|
|
|
|
97
|
|
|
let menu = Menu.buildFromTemplate(this.menuStructure); |
|
|
|
|
98
|
|
|
Menu.setApplicationMenu(menu); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
onRecentDocsChange(newVal, oldVal) { |
|
|
|
|
102
|
|
|
this.rebuildMenu(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|